import navTpl from './nav.tpl';
import itemTpl from './nav_item.tpl'
import './index.scss';

import tools from '../../utils/tools';

export default () => {
  return {
    name: 'nav',
    tpl (newsType) {
      const len = newsType.length,
            wrapper = (6 * len) + 'rem'; // 每个item项为width:6rem

      let navStr = '',
          itemsStr = '';

      navStr = navTpl().replace(tools.tplReplace(), wrapper); // nav添加宽度

      // 挂载item内容
      newsType.forEach((item, index) => {
        itemsStr += itemTpl().replace(tools.tplReplace(), (node, key) => {
          return {
            isCurrent: index === 0 ? 'current' : '',
            type: item.type,
            typeName: item.chs
          }[key]
        })
      });
      return {
        navStr,
        itemsStr
      }
    }
  }
}